package api;

import cn.hutool.json.JSONUtil;
import common.ApplicationVariable;
import model.ResultData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @description: 退出登录
 * @author: chengbaiyi
 * @create: 2022-06-26 14:28
 */
@WebServlet("/logout")
public class LoginOutServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int state = -1;
        HttpSession session = req.getSession(false);
        if (session != null && session.getAttribute(ApplicationVariable.SESSION_USER_KEY) != null) {
            session.removeAttribute(ApplicationVariable.SESSION_USER_KEY);
            state = 200;
        }
        resp.setContentType(ApplicationVariable.RESPONSE_CONTENT_TYPE);
        resp.getWriter().println(
                JSONUtil.toJsonStr(new ResultData(state, "", ""))
        );
    }
}
